package com.xyongfeng.foodManagement.domain;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.xyongfeng.foodManagement.domain.group.AddGroup;
import com.xyongfeng.foodManagement.domain.group.EditGroup;
import com.xyongfeng.foodManagement.domain.group.LoginGourp;
import com.xyongfeng.foodManagement.domain.group.NormalGroup;
import com.xyongfeng.foodManagement.domain.vo.AdminInfoVO;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.List;

/**
 * @TableName admin_info
 */
@TableName(value = "t_admin_info")
@Data
public class AdminInfo implements Serializable {
    /**
     * 管理员工号
     */
    @TableId(type = IdType.AUTO)
    private Integer adminId;

    /**
     * 管理员姓名
     */
    @Length(min = 2, max = 20, message = "姓名长度范围为2~20", groups = {NormalGroup.class})
    @NotBlank(message = "姓名不能为空", groups = {AddGroup.class})
    private String adminName;

    /**
     * 管理员联系电话
     */
    @Pattern(regexp = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$", message = "手机号格式错误", groups = {NormalGroup.class})
    @NotBlank(message = "联系电话不能为空", groups = {AddGroup.class})
    private String adminPhone;

    /**
     * 管理员账号
     */
    @Length(min = 4, max = 20, message = "账号长度范围为4~20", groups = {NormalGroup.class})
    @NotBlank(message = "账号不能为空", groups = {LoginGourp.class, AddGroup.class})
    private String adminUsername;

    /**
     * 管理员密码
     */
    @Length(min = 4, max = 20, message = "密码长度范围为4~20", groups = {NormalGroup.class})
    @NotBlank(message = "密码不能为空", groups = {LoginGourp.class, AddGroup.class})
    private String adminPassword;


    @TableField(exist = false)
    private List<String> roles;

    @TableField(exist = false)
    private List<String> permissions;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        AdminInfo other = (AdminInfo) that;
        return (this.getAdminName() == null ? other.getAdminName() == null : this.getAdminName().equals(other.getAdminName()))
                && (this.getAdminPhone() == null ? other.getAdminPhone() == null : this.getAdminPhone().equals(other.getAdminPhone()))
                && (this.getAdminUsername() == null ? other.getAdminUsername() == null : this.getAdminUsername().equals(other.getAdminUsername()))
                && (this.getAdminPassword() == null ? other.getAdminPassword() == null : this.getAdminPassword().equals(other.getAdminPassword()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getAdminId() == null) ? 0 : getAdminId().hashCode());
        result = prime * result + ((getAdminName() == null) ? 0 : getAdminName().hashCode());
        result = prime * result + ((getAdminPhone() == null) ? 0 : getAdminPhone().hashCode());
        result = prime * result + ((getAdminUsername() == null) ? 0 : getAdminUsername().hashCode());
        result = prime * result + ((getAdminPassword() == null) ? 0 : getAdminPassword().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", adminId=").append(adminId);
        sb.append(", adminName=").append(adminName);
        sb.append(", adminPhone=").append(adminPhone);
        sb.append(", adminUsername=").append(adminUsername);
        sb.append(", adminPassword=").append(adminPassword);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }

    public AdminInfoVO convertToVO() {
        AdminInfoVO adminInfoVO = new AdminInfoVO();
        BeanUtil.copyProperties(this, adminInfoVO);

        return adminInfoVO;
    }
}